package com.example.month11.fragment2.base;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentTransaction;

import com.example.month11.fragment2.utils.LocalManageUtil;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import static android.content.res.Configuration.ORIENTATION_LANDSCAPE;


/**
 * Activity基类。处理动态权限申请等逻辑。
 */
public abstract class BaseActivity extends AppCompatActivity {
    private static final String TAG = "BaseActivity";

    protected Context mContext;
    protected static int mGunIdx = 0; // 当前枪索引(0,1,2)

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
    }
    protected abstract void initViews();


    public FragmentTransaction getSupportFragmentTransaction() {
        return getSupportFragmentManager().beginTransaction();
    }
    protected <T extends BaseFragment> T findFragment(String tag) {
        return (T) getSupportFragmentManager().findFragmentByTag(tag);
    }
    public boolean onActivityBackPressed() {

        super.onBackPressed();
        return true;
    }


    @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(LocalManageUtil.setLocal(newBase));
    }

    protected void hideKeyboard(View view) {
        InputMethodManager imm = (InputMethodManager) view.getContext()
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }


    /**
     * 权限检查方法
     *
     * @param permissions 权限数组
     * @return
     */
    public boolean hasPermission(String... permissions) {
        for (String permission : permissions) {
            if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
        return true;
    }

    /**
     * 提供权限请求方法
     *
     * @param code
     * @param permissions
     */
    public void requestPermissions(int code, String... permissions) {
        ActivityCompat.requestPermissions(this, permissions, code);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case Constants.WRITE_EXTERNAL_CODE:
                doSDCardPermission();
                break;
            case Constants.WIFI_PERMISSION_CODE:
                doScanWifis();
                break;
            case Constants.COARSE_LOCATION_CODE:
                List<String> deniedList = new ArrayList<>(); // 遍历所有申请的权限，把被拒绝的权限放入集合
                for (int i = 0; i < grantResults.length; i++) {
                    int grantResult = grantResults[i];
                    if (grantResult == PackageManager.PERMISSION_GRANTED) {
//                       用户同意权限
                    } else {
                        deniedList.add(permissions[i]);
                    }
                }
                if (deniedList.isEmpty()) {
                    doCoarseLocation();
                } else {
                    // ToastUtil.toastShort(mContext, "请进入设置打开本软件需要的定位权限");
                }
                break;
            case Constants.CAMERA_CODE:
                doCameraPermission();
                break;
            case Constants.RECORD_AUDIO:
                doRecordAudioPermission();
                break;
        }
    }

    public void doRecordAudioPermission() {
    }

    // 父类提供默认的权限处理方法
    public void doSDCardPermission() {
    }

    public void doCoarseLocation() {
    }

    // 获得拍照，存储空间的访问权限
    public void doCameraPermission() {
    }

    public void doScanWifis() {
    }

    // 是否横屏。
    protected boolean isLandscape() {
        int orientation = getResources().getConfiguration().orientation;
        return orientation == ORIENTATION_LANDSCAPE;
    }

    protected static String getVersionName(Context mContext) {
        String version = null;
        try {
            PackageManager manager = mContext.getPackageManager();
            PackageInfo info = manager.getPackageInfo(mContext.getPackageName(), 0);
            version = info.versionName;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return version;
    }


    //endregion

}
